﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateNews : WebPart
    {
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditorShortContent;
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditorBody;
        public override string WebPartId
        {
            get
            {
                return "UpdateNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Cập nhật tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            string GroupNewsId = (string)System.Web.HttpContext.Current.Request["GroupNewsId"];
            string NewsId = (string)System.Web.HttpContext.Current.Request["NewsId"];
            NewsCls NewsCls = new NewsBLL().CreateModel(Environment.Environments.ActiveLanguage, NewsId);


            string SelectLanguageText =
              "<select id=divSelectLanguage onchange=javascript:DetectHtmlBody('" + NewsId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";


            FCKeditorShortContent = new FredCK.FCKeditorV2.FCKeditor();
            FCKeditorBody = new FredCK.FCKeditorV2.FCKeditor();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateNews));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "News.js") +
                             Environment.Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId, "UpdateNewsFormTitle", "Cập nhật tin tức")) +
                             "<div class=cssFormContent>" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsSubject", "Tiêu đề") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSubject value=\"" + NewsCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa bài viết") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=txtSearchKeyword>" + NewsCls.SearchKeyword + "</textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Source", "Nguồn") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSource value=\"" + NewsCls.Source + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsUrl", "Ảnh đặc trưng") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtImageUrl value=\"" + NewsCls.ImageUrl + "\"></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "ShortContent", "Mô tả") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=\"txtShortContent\">" + NewsCls.ShortContent + "</textarea></td>" +
                             "          </tr>" +

            //                 "          <tr>" +
                //                 "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"AddNewsShort","Mô tả") + "</td>" +
                //                 "              <td>";
                //pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
                //FCKeditorShortContent.Height = 400;
                //FCKeditorShortContent.Width = 780;
                //FCKeditorShortContent.Value = NewsCls.ShortContent;
                //FCKeditorShortContent.BasePath = "fckeditor/";
                //pl.Controls.Add(FCKeditorShortContent);

            //RetHtml="</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsBody", "Nội dung") + "</td>" +
                             "              <td class=cssTdFckEditor>";
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
            FCKeditorBody.Height = 800;
            FCKeditorBody.Value = NewsCls.Body;
            FCKeditorBody.BasePath = "fckeditor/";
            FCKeditorBody.Width = 780;
            pl.Controls.Add(FCKeditorBody);
            System.Web.HttpContext.Current.Session["AddNewsFckEditor"] = FCKeditorBody;
            RetHtml =
                             "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Date", "Ngày tạo") + ":</td>" +
                             "              <td>" + Environment.LookUpUtilities.GenerateLookUpDate("txtEntryDate", NewsCls.EntryDate) + "</td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "Attached", "Gắn kèm ?") + "</td>" +
                             "              <td>Ảnh <input class=cssAttached type=checkbox id=chkAttactedImage " + (NewsCls.HasImageAttached == 1 ? "checked" : "") + "> ~ Video <input type=checkbox class=cssAttached id=chkAttactedVideo " + (NewsCls.HasVideoAttached == 1 ? "checked" : "") + "> ~ Tài liệu <input type=checkbox class=cssAttached id=chkAttactedDoc " + (NewsCls.HasDocAttached == 1 ? "checked" : "") + "></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "EboVideo", "Nhúng video") + "</td>" +
                             "              <td><input id=\"txtEmObjectVideo\" value=\"" + NewsCls.EmbVideo + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "EboAudio", "Nhúng Audio") + "</td>" +
                             "              <td><input id=\"txtEmObjectAudio\" value=\"" + NewsCls.EmbAudio + "\"></td>" +
                             "          </tr>" +

                             "          <tr>" +
                             "              <td class=cssCol1></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveNews('" + GroupNewsId + "','" + NewsId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "AddNewsSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddNewsCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                             "</div>\r\n" + Environment.Environments.GetFooterFormDialog();
            //"<div id=\"divNewsRelations\">" + GetNewsRelationForm(NewsCls.belongSite, NewsCls) + "</div><div id=divAlreadyNewsRelation>"+GetAlreadyNewsRelation(NewsId)+"</div>\r\n" + Environment.Environments.GetFooterFormDialog();
            //"</div>";
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideGetGroupNewsItem(string belongSite, string SGroupNewsItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                string SelectGroupNewsText =
                   "<select id=drpSelectGroupNewsItem>";
                DataTable dtGroupNewsItems = new GroupNewsBLL().ReadingGroupNewsItemsForSearch(belongSite);
                for (int iIndex = 0; iIndex < dtGroupNewsItems.Rows.Count; iIndex++)
                {
                    string GroupNewsItemName = (string)dtGroupNewsItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    string GroupNewsItemId = (string)dtGroupNewsItems.Rows[iIndex]["GroupNewsItemId"];
                    if (GroupNewsItemId.Equals(SGroupNewsItemId))
                    {
                        SelectGroupNewsText += "<option selected value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                    else
                    {
                        SelectGroupNewsText += "<option  value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                }
                SelectGroupNewsText += "</select>";
                if (dtGroupNewsItems.Rows.Count == 0) SelectGroupNewsText = "?";
                return SelectGroupNewsText;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideNews(
            string ActiveLanguage,
            string NewsId,
            string Subject,
            string ImageUrl,
            string ShortContent,
            string Body,
            string SearchKeyword,
            string Source,
            string sEntryDate,
            bool AttachedDoc,
            bool AttachedImage,
            bool AttachedVideo,
            string EmObjectVideo,
            string EmObjectAudio
            )
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (Subject.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "EnterSubject", "Nhập tiêu đề!"));
                if (ShortContent.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "InputShortContent", "Nhập mô tả!"));
                if (Body.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "InputBody", "Nhập nội dung!"));

                ShortContent = System.Web.HttpUtility.HtmlDecode(ShortContent);
                Body = System.Web.HttpUtility.HtmlDecode(Body);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                new GroupNews().PermissionTypes, "GroupNewsItems");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, NewsCls.GroupNewsItemId);
                Environment.Permission.CheckPermission(
                    Environment.Environments.CurrentLoginUser.LoginName,
                    PermissionTypeCls.PermissionTypeCode,
                             "UpdateNewsItem", GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.Owner, true);

                bool CheckOk = Environment.FuncUtilities.checkVnDate(sEntryDate);
                if (CheckOk == false) throw new Exception(ResourceBLL.GetText(WebPartId, "InvalidDate", "Ngày tháng nhập sai!"));
                DateTime EntryDate = Environment.FuncUtilities.VNDateToDate(sEntryDate);

                string ShortContentForSearch = ShortContent;
                string BodyForSearch = Body;
                new NewsBLL().SaveNews(null, ActiveLanguage, GroupNewsItemCls.GroupNewsItemId, NewsId, Subject, ImageUrl, ShortContent, Body, SearchKeyword, Source, EntryDate, AttachedDoc ? 1 : 0, AttachedVideo ? 1 : 0, AttachedImage ? 1 : 0, EmObjectVideo, EmObjectAudio);
                return ResourceBLL.GetText(WebPartId, "MessageUpdateNewsOk", "Sửa tin tức thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetLoadDataMsg()
        {
            return ResourceBLL.GetText(WebPartId, "LoadingDataText", "Đang đọc dữ liệu...");
        }


        [AjaxPro.AjaxMethod]
        public string GetSubject(string ActiveLanguage, string NewsId)
        {
            try
            {
                NewsCls NewsCls = new NewsBLL().CreateModel(ActiveLanguage, NewsId);
                return NewsCls.Subject;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShortContent(string ActiveLanguage, string NewsId)
        {
            try
            {
                NewsCls NewsCls = new NewsBLL().CreateModel(ActiveLanguage, NewsId);
                return NewsCls.ShortContent;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetBody(string ActiveLanguage, string NewsId)
        {
            try
            {
                NewsCls NewsCls = new NewsBLL().CreateModel(ActiveLanguage, NewsId);
                return NewsCls.Body;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}